package com.jp.knowledge.my.activity;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.my.fragment.FeedbackFragment;

/**
 * Created by 528787067@qq.com on 2017/5/8.
 */

public class FeedbackActivity extends SlidingActivity implements View.OnClickListener {

    private static final int FEEDBACK_PUSH_CODE = 0x01;

    private FeedbackFragment fragment;
    private PopupWindow createPopupWindow;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == FEEDBACK_PUSH_CODE && resultCode == RESULT_OK)
            fragment.refreshData();
    }

    @Override
    protected int getResourceId() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void init() {
        initTitleBar();
        initPopupWindow();

        fragment = FeedbackFragment.newInstance(application.getUserData());
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.feedback_conentView, fragment)
                .commit();
    }

    private void initTitleBar(){
        topName.setText("意见反馈");
        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        rightIcon.setImageResource(R.mipmap.edit);
        leftIcon.setOnClickListener(this);
        rightIcon.setOnClickListener(this);
    }

    private void initPopupWindow(){
        View view = View.inflate(this, R.layout.team_circle_create_popupwindow, null);
        TextView feedBackTv = (TextView) view.findViewById(R.id.team_circle_create_dynamic);
        TextView errorCorrectionTv = (TextView) view.findViewById(R.id.team_circle_create_broke);
        feedBackTv.setText("反馈");
        errorCorrectionTv.setText("纠错");
        feedBackTv.setOnClickListener(this);
        errorCorrectionTv.setOnClickListener(this);
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        createPopupWindow = new PopupWindow(view, view.getMeasuredWidth(), view.getMeasuredHeight(), true);
        createPopupWindow.setOutsideTouchable(true);
        createPopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.icon_left:
                finish();
                break;
            case R.id.icon_serach:
                createPopupWindow.showAsDropDown(rightIcon,
                        -createPopupWindow.getWidth()+rightIcon.getWidth(), 0);
                break;
            case R.id.team_circle_create_dynamic:
                startActivityForResult(new Intent(mContext, FeedbackCreateActivity.class)
                        .putExtra("level", 1), FEEDBACK_PUSH_CODE);
                createPopupWindow.dismiss();
                break;
            case R.id.team_circle_create_broke:
                startActivityForResult(new Intent(mContext, FeedbackCreateActivity.class)
                        .putExtra("level", 2), FEEDBACK_PUSH_CODE);
                createPopupWindow.dismiss();
                break;
        }
    }
}
